package cn.iocoder.yudao.module.product.service.process;

import cn.iocoder.yudao.module.product.controller.admin.emissionsource.vo.EmissionSourceEPRespVO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.EmissionProcessCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.EmissionProcessRespVO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.EmissionProcessUpdateReqVO;
import cn.iocoder.yudao.module.product.convert.emissionsource.EmissionSourceConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.emissionsource.EmissionSourceDO;
import cn.iocoder.yudao.module.product.dal.dataobject.process.ProcessDO;
import cn.iocoder.yudao.module.product.dal.mysql.emissionsource.EmissionSourceMapper;
import cn.iocoder.yudao.module.product.dal.mysql.process.ProcessMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.product.dal.dataobject.process.EmissionProcessDO;

import cn.iocoder.yudao.module.product.convert.process.EmissionProcessConvert;
import cn.iocoder.yudao.module.product.dal.mysql.process.EmissionProcessMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.EMISSION_PROCESS_NOT_EXISTS;

/**
 * 排放源-工序关联 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class EmissionProcessServiceImpl implements EmissionProcessService {

    @Resource
    private EmissionSourceMapper emissionSourceMapper;

    @Resource
    private EmissionProcessMapper emissionProcessMapper;

    @Resource
    private ProcessMapper processMapper;

    @Override
    public Long createEmissionProcess(EmissionProcessCreateReqVO createReqVO) {
        // 插入
        EmissionProcessDO emissionProcess = EmissionProcessConvert.INSTANCE.convert(createReqVO);
        emissionProcessMapper.insert(emissionProcess);
        // 返回
        return emissionProcess.getId();
    }

    @Override
    public void updateEmissionProcess(EmissionProcessUpdateReqVO updateReqVO) {
        // 校验存在
        validateEmissionProcessExists(updateReqVO.getId());
        // 更新
        EmissionProcessDO updateObj = EmissionProcessConvert.INSTANCE.convert(updateReqVO);
        emissionProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmissionProcess(Long id) {
        // 校验存在
        validateEmissionProcessExists(id);
        // 删除
        emissionProcessMapper.deleteById(id);
    }

    private void validateEmissionProcessExists(Long id) {
        if (emissionProcessMapper.selectById(id) == null) {
            throw exception(EMISSION_PROCESS_NOT_EXISTS);
        }
    }

    @Override
    public EmissionProcessRespVO getEmissionProcess(Long id) {
        List<EmissionSourceEPRespVO> EmissionSourceEPRespVOList = new ArrayList<>();
        // 查工序
        ProcessDO processDO = processMapper.selectById(id);
        // 查工序关联表
        QueryWrapper<EmissionProcessDO>  queryWrapper0 = new QueryWrapper<>();
        List<EmissionProcessDO> emissionProcessDOList =  emissionProcessMapper.selectList(queryWrapper0.eq("process_id", id));
        // 查找所有排放源
        emissionProcessDOList.forEach(item -> {
            QueryWrapper<EmissionSourceDO> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(EmissionSourceDO::getId, item.getEmissionId());
            EmissionSourceDO emissionSourceDO = emissionSourceMapper.selectOne(queryWrapper1);
            // 将关联表id字段加入到排放源中
            EmissionSourceEPRespVO EmissionSourceEPRespVO = EmissionSourceConvert.INSTANCE.convertEPRespVO(emissionSourceDO);
            EmissionSourceEPRespVO.setEpId(item.getId());
            EmissionSourceEPRespVOList.add(EmissionSourceEPRespVO);
            }
        );
        EmissionProcessRespVO emissionProcessRespVO = new EmissionProcessRespVO();
        emissionProcessRespVO.setProcessDO(processDO);
        emissionProcessRespVO.setEmissions(EmissionSourceEPRespVOList);
        return emissionProcessRespVO;
    }

}
